Skip to content

feat(bedrock_agent): add new Amazon Bedrock Agents Functions Resolver #6564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from

Conversation

anafalcao
Copy link
Contributor

@anafalcao anafalcao commented Apr 25, 2025

Issue number:
#6300

Summary

This PR introduces support for Bedrock Agents Functions, providing the user a similar experience from the current Bedrock Agents with OpenAPI Resolver.

Changes

  • Bedrock Agent Functions data class
  • BedrockFunctionsResponseBuilder
  • BedrockAgentFunctionResolver
  • Optional response fields - sessionAttributes, promptSessionAttributes, knowledgeBasesConfiguration
  • Parser
  • Additional validations
  • name on Tool decorator
  • Documentation with examples

User experience

from aws_lambda_powertools.event_handler import BedrockAgentFunctionResolver
from aws_lambda_powertools.utilities.typing import LambdaContext

app = BedrockAgentFunctionResolver()

@app.tool(name="currentTime", description="Gets the current UTC time")
def get_current_time():
    ... 
@app.tool(description="Greets the user using name")
def greet_user(name):
...
@app.tool(description="Add two numbers")
def simple_calculator(a, b, operation):
...
def lambda_handler(event: dict, context: LambdaContext) → dict:
    return app.resolve(event, context)

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@boring-cyborg boring-cyborg bot added the tests label Apr 25, 2025
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Apr 25, 2025
@github-actions github-actions bot added the feature New feature or functionality label Apr 25, 2025
@anafalcao anafalcao self-assigned this Apr 25, 2025
@anafalcao anafalcao linked an issue Apr 25, 2025 that may be closed by this pull request
2 tasks
Copy link

codecov bot commented Apr 25, 2025

Codecov Report

Attention: Patch coverage is 98.55072% with 2 lines in your changes missing coverage. Please review.

Project coverage is 96.15%. Comparing base (24d6b4a) to head (39e0d36).
Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
...powertools/event_handler/bedrock_agent_function.py 96.87% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #6564      +/-   ##
===========================================
+ Coverage    96.12%   96.15%   +0.02%     
===========================================
  Files          253      255       +2     
  Lines        12104    12240     +136     
  Branches       902      909       +7     
===========================================
+ Hits         11635    11769     +134     
- Misses         369      370       +1     
- Partials       100      101       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pull-request-size pull-request-size bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Apr 25, 2025
@anafalcao anafalcao changed the title feat(bedrock_agent): create bedrock agents functions data class feat(bedrock_agent): Add new Amazon Bedrock Agents Functions Apr 25, 2025
@anafalcao anafalcao changed the title feat(bedrock_agent): Add new Amazon Bedrock Agents Functions feat(bedrock_agent): WIP - Add new Amazon Bedrock Agents Functions Apr 25, 2025
@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels May 1, 2025
@pull-request-size pull-request-size bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 2, 2025
@anafalcao anafalcao changed the title feat(bedrock_agent): WIP - Add new Amazon Bedrock Agents Functions feat(bedrock_agent): add new Amazon Bedrock Agents Functions Resolver May 6, 2025
@anafalcao anafalcao marked this pull request as ready for review May 6, 2025 13:49
@anafalcao anafalcao requested a review from a team as a code owner May 6, 2025 13:49
Copy link

sonarqubecloud bot commented May 8, 2025

Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @anafalcao! I finished the first review and we need to fix the parameters part before proceeding with another review. Can you please work on that?

"response": {
"actionGroup": event.action_group,
"function": event.function,
"functionResponse": {"responseBody": {"TEXT": {"body": str(body if body is not None else "")}}},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that respondeBody expects a dynamic MIME type instead of hard-coded "TEXT". Do you know what happens when you change this and return a json, for example?

image

response_state: str | None = None,
) -> None:
if response_state is not None and response_state not in ["FAILURE", "REPROMPT"]:
raise ValueError("responseState must be None, 'FAILURE' or 'REPROMPT'")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise ValueError("responseState must be None, 'FAILURE' or 'REPROMPT'")
raise ValueError("responseState must be 'FAILURE' or 'REPROMPT'")

session_attributes: dict[str, str] | None = None,
prompt_session_attributes: dict[str, str] | None = None,
knowledge_bases: list[dict[str, Any]] | None = None,
response_state: str | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes more sense this field be a Literal of FAILURE AND REPROMPT and then give the customer a type hint in their IDE.

Comment on lines +150 to +151
if not description:
raise ValueError("Tool description is required")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description parameter can be "None", but is it mandatory here? Is the description mandatory to register a route? If not, I think we should not check this here.

Comment on lines +154 to +155
if function_name in self._tools:
raise ValueError(f"Tool '{function_name}' already registered")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I don't think we should raise here, but instead warning and register the most recent route.

function_name = self.current_event.function

try:
result = self._tools[function_name]["function"]()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A critical part of this agent's workflow is passing parameters to functions. These parameters contain values ​​that will be useful for the function in question. In this discussion, we talk about this:

Support for injecting parameters into the function signature.

Can you pls add this support?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event_handlers feature New feature or functionality size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Amazon Bedrock Agents Functions
2 participants